package sort;

/**
 * 简单选择排序
 * 每次循环中选取关键字最小的记录
 * @author mengxin
 *
 */
public class SelectionSort {
	public static void sort(int[] values) {
		int temp;
		int offset = 0;
		for (int i = 0; i < values.length; i++) {
			temp = values[i];
			offset = 0;// offset指到每一次排序中最小元素的位置
			for (int j = i + 1; j < values.length; j++) {
				if (temp > values[j]) {
					temp = values[j];
					offset = j;
				}
			}
			if (offset > i) {
				values[offset] = values[i];
				values[i] = temp;
			}
			System.out.print("第" + (i + 1) + "次：");
			for (int k = 0; k < values.length; k++) {
				System.out.print(values[k] + ",");
			}
			System.out.println("");
		}
	}

	public static void main(String[] args) {
		int[] values = { 5, 2, 4, 1, 3 };
		sort(values);
		for (int i = 0; i < values.length; ++i) {
			System.out.println(values[i]);
		}
	}
}
